package com.ydche.util;

public class StringUtil {
    static final char[] digits = new char[]{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};

    public StringUtil() {
    }

    public static String toHexString(byte[] bytes) {
        char[] buf = new char[bytes.length * 2];
        int radix = 16;
        int mask = radix - 1;

        for(int i = 0; i < bytes.length; ++i) {
            buf[2 * i] = digits[bytes[i] >>> 4 & mask];
            buf[2 * i + 1] = digits[bytes[i] & mask];
        }

        return new String(buf);
    }

    public static byte[] hexStringToByteArray(String hex) {
        if (hex == null) {
            return null;
        } else {
            String[] bss = hex.split(" ");
            hex = "";
            String[] arr$ = bss;
            int len$ = bss.length;

            int j;
            for(j = 0; j < len$; ++j) {
                String bs = arr$[j];
                hex = hex + bs;
            }

            int stringLength = hex.length();
            if (stringLength % 2 != 0) {
                throw new IllegalArgumentException("Hex String must have even number of characters!");
            } else {
                byte[] result = new byte[stringLength / 2];
                j = 0;

                for(int i = 0; i < result.length; ++i) {
                    char hi = Character.toLowerCase(hex.charAt(j++));
                    char lo = Character.toLowerCase(hex.charAt(j++));
                    result[i] = (byte)(Character.digit(hi, 16) << 4 | Character.digit(lo, 16));
                }

                return result;
            }
        }
    }

    public static boolean isNULLorEmpty(String str) {
        return str == null || "".equals(str) || str.length() == 0;
    }

}
